﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QLKHACHHANG.DataAccess
{
    class DBStaff
    {
        //Ham update thong tin nhan vien
        public static bool UpdateStaff(
            string Manhanvien,
            string Tennhanvien, 
            string Tendangnhap,
            string Matkhau, 
            string Chucvu,                                       
            string Diachi, 
            string Sodienthoai, 
            string Email, 
            string Mabophan, 
            int Trangthai,
            int Luachon)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_SuaNhanVien", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Manhanvien", Manhanvien);
                cmd.Parameters.AddWithValue("@Tennhanvien", Tennhanvien);
                cmd.Parameters.AddWithValue("@Tendangnhap", Tendangnhap);
                cmd.Parameters.AddWithValue("@Matkhau", Matkhau);
                cmd.Parameters.AddWithValue("@Chucvu", Chucvu);
                cmd.Parameters.AddWithValue("@Diachi", Diachi);
                cmd.Parameters.AddWithValue("@Sodienthoai", Sodienthoai);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@Mabophan", Mabophan);
                cmd.Parameters.AddWithValue("@Trangthai", Trangthai);
                cmd.Parameters.AddWithValue("@Luachon", Luachon);    
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }            
            catch (Exception ex)
            {
                if(ex.Message.Contains("@@Lỗi"))
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);                    
                }
               
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
        // chen 1 nhan vien vao csdl    
        public static bool InsertStaff(
            string Manhanvien,
            string Tennhanvien, 
            string Tendangnhap,
            string Matkhau, 
            string Chucvu,                                       
            string Diachi, 
            string Sodienthoai, 
            string Email, 
            string Mabophan, 
            int Trangthai
            )
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_ThemNhanVien", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Manhanvien", Manhanvien);
                cmd.Parameters.AddWithValue("@Tennhanvien", Tennhanvien);
                cmd.Parameters.AddWithValue("@Tendangnhap", Tendangnhap);
                cmd.Parameters.AddWithValue("@Matkhau", Matkhau);
                cmd.Parameters.AddWithValue("@Chucvu", Chucvu);
                cmd.Parameters.AddWithValue("@Diachi", Diachi);
                cmd.Parameters.AddWithValue("@Sodienthoai", Sodienthoai);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@Mabophan", Mabophan);
                cmd.Parameters.AddWithValue("@Trangthai", Trangthai);
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("@@Lỗi"))
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
               
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
        // xoa 1 nhan vien tu csdl
        public static bool DeleteStaff(string Manhanvien)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            bool status = false;
            try
            {
                string connectionstring = SetConfig.getAppSetting("ConnectionString");
                conn = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand("sp_XoaNhanVien", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Manhanvien", Manhanvien);
                conn.Open();
                rdr = cmd.ExecuteReader();
                status = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại kết nối CSDL !\n\n" + "(Mã lỗi:" + ex.Message + ")");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return status;
        }
    }
}
